package com.hellobike.haxing.modules.statics.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.modules.statics.dto.*;
import com.hellobike.haxing.modules.statics.mapper.OrderDetailStaticsMapper;
import com.hellobike.haxing.modules.statics.param.CategorySalesParam;
import com.hellobike.haxing.modules.statics.param.OrderDetailParam;
import com.hellobike.haxing.modules.statics.param.ShopSalesStaticsParam;
import com.hellobike.haxing.modules.statics.service.IOrderDetailStaticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class OrderDetailStaticsServiceImpl implements IOrderDetailStaticsService {

    @Autowired
    private OrderDetailStaticsMapper orderDetailStaticsMapper;

    @Override
    public IPage<OrderDetailStatics> select(OrderDetailParam orderDetailParam, Integer pageNo, Integer pageSize) {
        IPage<OrderDetailStatics> page = new Page<>(pageNo, pageSize);
        return orderDetailStaticsMapper.selectPage(page, orderDetailParam);
    }

    @Override
    public IPage<CategorySalesStatics> selectCategory(CategorySalesParam categorySalesParam, Integer pageNo, Integer pageSize) {
        IPage<CategorySalesStatics> page = new Page<>(pageNo, pageSize);
        return orderDetailStaticsMapper.selectCategoryPage(page, categorySalesParam);
    }

    @Override
    public IPage<ShopSalesStatics> selectShop(ShopSalesStaticsParam shopSalesStaticsParam, Integer pageNo, Integer pageSize) {
        IPage<ShopSalesStatics> page = new Page<>(pageNo, pageSize);
        return orderDetailStaticsMapper.selectShopSalesStatis(page, shopSalesStaticsParam);
    }

    @Override
    public IPage<ProductStats> selectProduct(OrderDetailParam orderDetailParam, Integer pageNo, Integer pageSize) {
        IPage<ProductStats> countPage = new Page<>(pageNo, pageSize);
        IPage<ProductStats> amountPage = new Page<>(pageNo, pageSize);
        IPage<ProductStats> productCountStats = orderDetailStaticsMapper.selectProductCountStats(countPage, orderDetailParam);
        IPage<ProductStats> productAmountStats = orderDetailStaticsMapper.selectProductAmountStats(amountPage, orderDetailParam);
        if (productAmountStats.getRecords().size() > productCountStats.getRecords().size()) {
            for (int i = 0; i < productCountStats.getRecords().size(); i++) {
                ProductStats productStats = productCountStats.getRecords().get(i);
                ProductStats productStats1 = productAmountStats.getRecords().get(i);
                productStats1.setCountTitle(productStats.getCountTitle());
                productStats1.setProductCount(productStats.getProductCount());
            }
            return productAmountStats;
        } else {
            for (int i = 0; i < productAmountStats.getRecords().size(); i++) {
                ProductStats productStats1 = productCountStats.getRecords().get(i);
                ProductStats productStats = productAmountStats.getRecords().get(i);
                productStats1.setAmountTitle(productStats.getAmountTitle());
                productStats1.setProductAmount(productStats.getProductAmount());
            }
            return productCountStats;
        }
    }

    @Override
    public IPage<OrderPayStats> selectPay(OrderDetailParam orderDetailParam, Integer pageNo, Integer pageSize) {
        IPage<OrderPayStats> page = new Page<>(pageNo, pageSize);
        IPage<OrderPayStats> orderPayPage = orderDetailStaticsMapper.selectOrderPay(page, orderDetailParam);
        return orderPayPage;
    }
}
